<?php 

Class wxkeyModel extends appModel
{
	
	
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('article')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	
	/**
	 * 删除关键字
	 * @param $id
	 * @return 
	 */
	public function del_key($id)
	{
		$result = array('error' => 0, 'content' => '删除成功');
		if($id)
		{
			$sql = "DELETE FROM ".$this->table('wxkeyword')." WHERE  id='".$id."'";
			$res = $this->query($sql);
			if(!$res)
			{
				$result['error'] = 1;
				$result['content'] = '删除失败';
			}
		}
		return $result;
	}
	
	/**
	 * 获取关键字信息
	 * @param $id
	 * @return 
	 */
	public function get_key($id)
	{
		if($id)
		{
		  	$sql = "SELECT a.*,(SELECT b.name FROM ".$this->table('rule')." as b WHERE a.rid=b.id) as rule_name FROM ".$this->table('wxkeyword')." as a WHERE a.id = '".$id."'";
			$result = $this->getRow($sql);
		  	if($result)
		  	{
				return $result;
		  	}
		  	else
		  	{
				return $result = array();
		  	}
		}
	}
	
	/**
	 * 更新关键字信息
	 * @param $info
	 * @param $id
	 */
	public function update_key($info, $id)
	{
		if($id)
		{
			$data = yf_set_update($info);
			$sql="UPDATE ".$this->table('wxkeyword')." SET ".$data." WHERE id='".$id."'";
			return $res=$this->query($sql);
		}
	}
	
	/**
	 * 创建关键字或绑定
	 * @param unknown $info
	 * @return multitype:number string Ambigous <boolean, void, multitype:, string>
	 */
	public function create_key($info)
	{
		$result = array('error' => 0, 'content' => '');
		
		$data = yf_set_insert($info);
		$sql = "INSERT INTO ".$this->table('wxkeyword')." (".$data['key'].") VALUES(".$data['val'].")";
		if(!$res = $this->query($sql))
		{
			$result['error'] = 1;
			$result['content'] = Lang('SQL_ERROR');
		}
		return $result;
	}
	
	/**
	 * 获得关键字列表
	 * @param $page
	 * @param $sort
	 * @param $limit
	 * @return 
	 */
	public function key_list($page = 1, $sort = array(), $limit = 10)
	{
		$listPage = array('data' => '', 'pageLink' => '');
		$where = ' WHERE 1=1';
		if($sort['keyword'] != '')
		{
			$where .= " AND keyword like '%".$sort['keyword']."%'";
		}
		$result = $this->show_page("SELECT id,keyword FROM ".$this->table('wxkeyword').$where.' ORDER BY id DESC', $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		return $listPage;
	}

	/**
	 * 确认关键字是否存在
	 * @param $code
	 * @param $id
	 * @return boolean
	 */
	public function check_key($code, $id = 0)
	{
		if($id > 0)
		{
			$sql = "SELECT keyword FROM ".$this->table('wxkeyword')." WHERE id='".$id."'";
			$oldcode=$this->getOne($sql);
			$code_exist=0;
			if($oldcode!=$code)
			{
				$sql="SELECT count(*) FROM ".$this->table('wxkeyword')." WHERE keyword='$code'";
				$code_exist=$this->getOne($sql);
					
			}
			
			if($code_exist>0)
			{
				
				return false;
			}
			else 
			{
				return true;
			}
		}
		else 
		{
		    $sql="SELECT count(*) FROM ".$this->table('wxkeyword')." WHERE keyword='$code'";
		    if($this->getOne($sql) > 0)
		   	{
				return false;
		   	}
		   	else 
		  	{
				return true;
		  	}
		}
	}
	
	
}